package com.xms.ribbonbutton.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.UpdaterService;
import com.xms.event.core.AuthorizationStateChangedEvent;
import com.xms.event.core.IEventPublisher;
import com.xms.ribbonbutton.core.RibbonButtonDefaults;
import com.xms.ribbonbutton.entity.RibbonButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * RibbonButtonUpdaterService
 * </p>
 *
 * @author migo
 * @since 2021/9/8
 */
@CacheConfig(cacheNames = "ribbonbutton", keyGenerator = "CacheKeyGenerator")
@Service
public class RibbonButtonUpdaterService extends UpdaterService<RibbonButton> implements IRibbonButtonUpdaterService {
    private IEventPublisher _eventPublisher;
    private IRibbonButtonDependency _ribbonButtonDependency;

    @Autowired
    public RibbonButtonUpdaterService(IEventPublisher eventPublisher,
                                      IRibbonButtonDependency ribbonButtonDependency) {
        _eventPublisher = eventPublisher;
        _ribbonButtonDependency = ribbonButtonDependency;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(RibbonButton entity) throws XmsException, SQLException {
        boolean success = super.updateById(entity);
        if(success){
            _ribbonButtonDependency.update(entity);
        }
        return success;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateMany(List<RibbonButton> entityList) throws XmsException, SQLException {
        for (RibbonButton entity : entityList) {
            this.update(entity);
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateState(boolean isEnabled, List<UUID> id) {
        Assert.notEmpty(id, "id must not be empty");
        UpdateWrapper<RibbonButton> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(RibbonButton::getStatecode, isEnabled)
                .in(RibbonButton::getRibbonbuttonid, id);
        return super.update(updateWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateAuthorization(boolean isAuthorization, List<UUID> id) {
        Assert.notEmpty(id, "id must not be empty");
        UpdateWrapper<RibbonButton> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(RibbonButton::getAuthorizationenabled, isAuthorization)
                .in(RibbonButton::getRibbonbuttonid, id);
        boolean success = super.update(updateWrapper);
        if (success) {
            _eventPublisher.publish(new AuthorizationStateChangedEvent(RibbonButtonDefaults.MODULE_NAME, id, isAuthorization));
        }
        return success;
    }
}
